package com.example.cainiaowo_xqx

import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.cainiaowo_xqx.databinding.ActivityMainBinding
import com.example.common.base.BaseActivity
import com.example.common.widget.BnvVp2Mediator
import com.example.course.CourseFragment
import com.example.home.HomeFragment
import com.example.mine.MineFragment
import com.example.study.StudyFragment

class MainActivity : BaseActivity<ActivityMainBinding>() {

    override fun getLayoutRes() = R.layout.activity_main

    //可以起到复用的效果，首次get对象会初始化，再次get会获取对象的值
    //将索引和fragment做一个映射关系
    private val fragments = mapOf<Int, Fragment>(
        INDEX_HOME to HomeFragment(),
        INDEX_COURSE to CourseFragment(),
        INDEX_STUDY to StudyFragment(),
        INDEX_MINE to MineFragment()
    )

    override fun initConfig() {
        super.initConfig()
    }

    override fun initData() {
        super.initData()
    }

    override fun initView() {
        super.initView()
//        val navController = findNavController(R.id.fcv_main)
//        mBinding.bnvMain.setupWithNavController(navController)
        mBinding.apply {
            //viewpage2数据适配 初始化
            vp2Main.adapter = MainViewPagerAdapter(this@MainActivity, fragments)
            //将bnv和vp2事件绑定
            BnvVp2Mediator(bnvMain, vp2Main) { bnv, vp2 ->
                //viewpager不需要滑动
                vp2.isUserInputEnabled = false
            }.attach()
        }
    }

    //构造函数没有val代表传参，并不属于类内部的成员变量，加了val内部可以成为类的内部成员属性
    class MainViewPagerAdapter(
        fragmentActivity: FragmentActivity,
        private val fragments: Map<Int, Fragment>
    ) : FragmentStateAdapter(fragmentActivity) {
        override fun getItemCount() = fragments.size

        override fun createFragment(position: Int): Fragment {
            return fragments[position] ?: error("亲确保fragment数据源和viewpager2的index相互匹配")
        }

    }


    //定义一些抽象的常量
    companion object {
        const val INDEX_HOME = 0//首页home对应的
        const val INDEX_COURSE = 1//
        const val INDEX_STUDY = 2//
        const val INDEX_MINE = 3//
    }
}